//
//  AppDelegate.swift
//  YJWeiBo
//
//  Created by Jim1024 on 2017/1/9.
//  Copyright © 2017年 Jim1024. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        setAppearance()
        
        window = UIWindow(frame: UIScreen.main.bounds)
        
        window?.rootViewController = defaultViewController()
        
        // MARK: - test ViewController
//        window?.rootViewController = UINavigationController(rootViewController: YJOauthViewController())
//        window?.rootViewController = YJWelcomeViewController()
        
        
        window?.makeKeyAndVisible()
        
        return true
    }
    
    /**
     界面切换,不需要介入,直接可以判断的
     
     - returns: 默认的控制器
     */
    fileprivate func defaultViewController() -> UIViewController {
        if !YJUserAccountViewModel.sharedInstance.userLogin {
            // 没有登陆
            return YJMainViewController()
        }
        
        // 登陆过了
        return isNewVerson() ? YJNewFeatureViewController() : YJWelcomeViewController()
    }
    
    /// 外部调用的切换根控制器方法
    class func outSwitchRootViewController(_ controller: UIViewController) {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window?.rootViewController = controller
    }

    /**
     是否是新版本
     
     - returns: 是否是新版本
     */
    fileprivate func isNewVerson() -> Bool {
        // 1.获取当前版本
        let version = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        //        print("version: \(version)")
        
        // 2.获取上一次的版本
        let sandboxKey = "sandboxVersionKey"
        let sandboxVersion = UserDefaults.standard.string(forKey: sandboxKey)
        //        print("sandboxVersion: \(sandboxVersion)")
        
        // 3.对比, 不一样就是新版本
        let isNewVersion = version != sandboxVersion
        
        // 4.是新版本,保存当前版本号
        if isNewVersion {
            UserDefaults.standard.set(version, forKey: sandboxKey)
            UserDefaults.standard.synchronize()
        }
        
        // 5.返回比较结果
        return isNewVersion
    }
    
    /// 设置皮肤
    fileprivate func setAppearance() {
        // 设置导航栏皮肤
        UINavigationBar.appearance().tintColor = UIColor.orange
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // 清除一个星期之前缓存到本地的微博数据
        YJStatusDAL.shared.clearCacheData()
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

